package com.yanqu.road.dao.impl.channelcenter.washe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.washe.data.WasheHistoryRank;
import com.yanqu.road.entity.rank.xrank.XSeasonUserRank;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WasheHistoryRankDaoImpl extends TempDataObjectDao<WasheHistoryRank> {

    public static String getWasheHistoryRankSql = "select * from `t_u_washe_history_rank` ";

    public Map<Integer,Map<Integer, WasheHistoryRank>> getWasheHistoryRankMap() {

        Map<Integer,Map<Integer, WasheHistoryRank>> map = new ConcurrentHashMap<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWasheHistoryRankSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                WasheHistoryRank parse = parse(rs);
                Map<Integer, WasheHistoryRank> rankMap = map.getOrDefault(parse.getSeasonId(), new ConcurrentHashMap<>());
                rankMap.put(parse.getRankType(), parse);
                map.put(parse.getSeasonId(), rankMap);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateWasheHistoryRankSql = "update `t_u_washe_history_rank` set userRankMap=?,topRank=?,autoSendReward=?,rankType=? where seasonId=?";

    @Override
    protected boolean updateData(WasheHistoryRank washeHistoryRank) {
        washeHistoryRank.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistoryRank.getUserRankMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, washeHistoryRank.getTopRank() == null ? "" : JSON.toJSONString(washeHistoryRank.getTopRank())));
        para.put(i++, new DbParameter(Types.BOOLEAN, washeHistoryRank.isAutoSendReward()));
        para.put(i++, new DbParameter(Types.INTEGER, washeHistoryRank.getRankType()));
        para.put(i++, new DbParameter(Types.INTEGER, washeHistoryRank.getSeasonId()));
        if (getDb().executeNoneQuery(updateWasheHistoryRankSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWasheHistoryRankSql = "insert into `t_u_washe_history_rank`(seasonId,userRankMap,topRank,autoSendReward,rankType) values (?,?,?,?,?)";

    @Override
    protected boolean addData(WasheHistoryRank washeHistoryRank) {
        washeHistoryRank.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, washeHistoryRank.getSeasonId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistoryRank.getUserRankMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, washeHistoryRank.getTopRank() == null ? "" : JSON.toJSONString(washeHistoryRank.getTopRank())));
        para.put(i++, new DbParameter(Types.BOOLEAN, washeHistoryRank.isAutoSendReward()));
        para.put(i++, new DbParameter(Types.INTEGER, washeHistoryRank.getRankType()));
        if (getDb().executeNoneQuery(insertWasheHistoryRankSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected WasheHistoryRank parseData(ResultSet rs) throws SQLException {
        WasheHistoryRank washeHistoryRank = new WasheHistoryRank();
        washeHistoryRank.setSeasonId(rs.getInt("seasonId"));
        washeHistoryRank.setUserRankMap(JSON.parseObject(rs.getString("userRankMap"), new TypeReference<ConcurrentHashMap<Long, Integer>>() {
        }));
        String topRank = rs.getString("topRank");
        if (!StringUtils.isNullOrEmpty(topRank)) {
            washeHistoryRank.setTopRank((JSON.parseObject(topRank, new TypeReference<XSeasonUserRank>() {
            })));
        }
        washeHistoryRank.setAutoSendReward(rs.getBoolean("autoSendReward"));
        washeHistoryRank.setRankType(rs.getInt("rankType"));
        return washeHistoryRank;
    }

}
